/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.J2SE.ZIP;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author kylin
 */
public class TestZIp {

    static final int BUFFER = 2048;

    public static void main(String[] args) {
        String fileName = "/home/kylin/Shared/KGFileuploader1.0.zip";
        String unZipDir = "/home/kylin/Shared/unzip";
        try {
            unZip(fileName,unZipDir);
        } catch (IOException ex) {
            Logger.getLogger(TestZIp.class.getName()).log(Level.SEVERE, null, ex);
        }
//        try {
//            File dir=new File("/home/kylin/Shared/KGFileuploader1.0");
//            zip("/home/kylin/Shared/KGFileuploader1.0.zip","/home/kylin/Shared/HxDchs.zip");
//        } catch (IOException ex) {
//            Logger.getLogger(TestZIp.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    public static void unZip(String fileName, String unZipDir) throws IOException {
         try {
            File f = new File(unZipDir);
            if (!f.exists()) {
                f.mkdirs();
            }
            BufferedOutputStream dest = null;
            BufferedInputStream is = null;
            ZipEntry entry;
            ZipFile zipfile = new ZipFile(fileName);
            Enumeration e = zipfile.entries();
            while (e.hasMoreElements()) {
                entry = (ZipEntry) e.nextElement();
                if (entry.isDirectory()) {
                    makeDir(unZipDir + File.separator + entry.getName());
                } else {
                    is = new BufferedInputStream(zipfile.getInputStream(entry));
                    int count;
                    byte data[] = new byte[BUFFER];
                    FileOutputStream fos = new FileOutputStream(unZipDir + File.separator
                            + entry.getName());
                    // System.out.println("entry.getName(): " +
                    // entry.getName());
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                    is.close();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static boolean makeDir(String unZipDir) {
        boolean b = false;
        try {
            File f = new File(unZipDir);
            if (!f.exists()) {
                b = f.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return b;
        }
        return b;
    }

    private static String filterZip(String fileName) {
        if (fileName.endsWith("zip")) {
            fileName = fileName.substring(0, fileName.length() - 4);
        }
        return fileName;
    }
    

    public static void zip(String fileName, String... files) throws IOException {
        //构造压缩文件输出流
        ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(fileName));
        for(int i=0,size=files.length;i<size;i++){
            //创建压缩实体
            ZipEntry entry=new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
            System.out.println("entry:"+entry.getName());
            zos.putNextEntry(entry);
            //将文件内容输出到压缩流中
            System.out.println(files[i]);
            InputStream is = new FileInputStream(files[i]);
            int count = 0;
            byte[] buffer = new byte[1024];
            while ((count = is.read(buffer)) >= 0) {
                zos.write(buffer, 0, count);
            }
            zos.flush();
            zos.closeEntry();
            is.close();
        }
    }
}
